from rest_framework import viewsets
from .serializers import LearnSerializer
from .models import Learn
from rest_framework.response import Response
from utils.filters import LearnFilter

class LearnViewSet(viewsets.ModelViewSet):
    # 查询所有数据
    queryset = Learn.objects.all()
    # 配置序列化器
    serializer_class = LearnSerializer
    # filter_backends = (filters.DjangoFilterBackend,)
    # # 配置需要过滤的名字
    filterset_class = LearnFilter
    # # 只有管理员才能操作
    # permission_classes = [IsAdminUserOrReadOnly]
    def get_paginated_response(self, data):
        # 当size=all时返回原始数组结构
        if self.request.query_params.get('data', '').lower() == 'all':
            return Response(data)
        return super().get_paginated_response(data)
